package com.hmis.hmis.UserInterface.systemUI;

import com.hmis.hmis.UserInterface.checkController.Check;
import com.hmis.hmis.UserInterface.managementUI.Management;
import com.hmis.hmis.UserInterface.publishUI.Publish;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.Objects;


public class SystemUI {

    private static SystemUI systemUI;
    private Stage stage = new Stage();
    private BorderPane systemMain = new BorderPane();
    private BorderPane top = new BorderPane();
    private BorderPane left = new BorderPane();
    private ScrollPane scrollPane = new ScrollPane();//中心面板
    private StackPane center = new StackPane();
    private BorderPane right = new BorderPane();
    private BorderPane bottom = new BorderPane();
    private Scene scene;
    private Label labelCenter = new Label("首页");
    private Label mainButton = new Label("首页");
    private Label registerButton = new Label("登记住院患者");
    private Label managementButton = new Label("住院患者管理");
    private Label checkButton = new Label("患者检查模块");
    private Label diagnoseButton = new Label("患者诊断模块");
    private Label exitButton = new Label("退出系统");

    private SystemUI() {
        initTop();
        initLeft();
        initcenter();
        initRight();
        initBottom();
        initButtons();
        systemMain.setPadding(new Insets(10));
        systemMain.setTop(top);
        systemMain.setLeft(left);

        systemMain.setCenter(scrollPane);
        scrollPane.setContent(getCenter());
        center.prefWidthProperty().bind(scrollPane.maxWidthProperty());
        center.prefHeightProperty().bind(scrollPane.maxHeightProperty());

        systemMain.setRight(right);
        systemMain.setBottom(bottom);
        //#99CCFF
        systemMain.setStyle("-fx-background-color: #CCFFCC");


        scene = new Scene(systemMain, 1250, 800);
        stage.setScene(scene);
    }

    public static SystemUI getSystemUI() {
        if (systemUI == null) {
            systemUI = new SystemUI();
        }
        return systemUI;
    }


    private void initTop() {
        VBox vbCenter = new VBox(10);
        vbCenter.setAlignment(Pos.CENTER);
        vbCenter.setPrefHeight(50);
        labelCenter.setStyle("-fx-font: 40 arial");
        vbCenter.getChildren().add(labelCenter);
        top.setCenter(vbCenter);
    }

    private void initLeft() {
        VBox functions = new VBox(40);
        functions.setAlignment(Pos.CENTER);
        functions.getChildren().addAll(mainButton, registerButton, managementButton, checkButton, diagnoseButton, exitButton);
        left.setCenter(functions);
        Label lbleft = new Label("");
        lbleft.setPrefWidth(15);
        left.setLeft(lbleft);
        Label lbright = new Label("");
        lbright.setPrefWidth(15);
        left.setRight(lbright);

    }

    private void initcenter() {
//        center.setBorder(new Border(new BorderStroke(Paint.valueOf("#283434"),Paint.valueOf("#283434"), Paint.valueOf("#283434"),Paint.valueOf("#283434"),
//                BorderStrokeStyle.SOLID, BorderStrokeStyle.SOLID,BorderStrokeStyle.SOLID,BorderStrokeStyle.SOLID,null,
//                new BorderWidths(1,1,1,1),null)));
        labelCenter.setText("首页");
        center.getChildren().clear();
        System.out.println("加载首页");
        //加载.fxml
        AnchorPane anchorPane = new AnchorPane();
        try {
            anchorPane = FXMLLoader.load(Objects.requireNonNull(SystemUI.class.getResource("/fxml/mainUI.fxml")));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        center.getChildren().add(anchorPane);
    }

    private void initRight() {
        systemMain.setRight(null);
    }

    private void initBottom() {
        VBox vb1 = new VBox(25);
        Label lb1 = new Label("");
        lb1.setPrefHeight(30);
        vb1.setAlignment(Pos.CENTER);
        vb1.getChildren().addAll(lb1);
        bottom.setCenter(vb1);
    }

    private void initButtons() {

        mainButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                labelCenter.setText("首页");
                center.getChildren().clear();
                System.out.println("加载首页");
                //加载.fxml
                AnchorPane anchorPane = new AnchorPane();
                try {
                    anchorPane = FXMLLoader.load(Objects.requireNonNull(SystemUI.class.getResource("/fxml/mainUI.fxml")));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                center.getChildren().add(anchorPane);

            }
        });

        registerButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                labelCenter.setText("登记住院患者");
                center.getChildren().clear();
                center.getChildren().addAll(Publish.getPublish().getMain());
                Publish.getPublish().getMain().prefWidthProperty().bind(center.prefWidthProperty());
                Publish.getPublish().getMain().prefHeightProperty().bind(center.prefHeightProperty());
                Publish.getPublish().refreshUI();

            }
        });

        managementButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                labelCenter.setText("住院患者管理");
                center.getChildren().clear();
                center.getChildren().addAll(Management.getManagement().getStackPane());
            }
        });

        checkButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                labelCenter.setText("患者检查模块");
                center.getChildren().clear();
                center.getChildren().addAll(Check.getCheck().gethBox());
                Check.getCheck().gethBox().prefWidthProperty().bind(center.prefWidthProperty());
                Check.getCheck().gethBox().prefHeightProperty().bind(center.prefHeightProperty());
            }
        });

        diagnoseButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                labelCenter.setText("患者诊断模块");
                center.getChildren().clear();
                //加载diagnoseUI.fxml
                AnchorPane anchorPane = new AnchorPane();
                try {
                    anchorPane = FXMLLoader.load(Objects.requireNonNull(SystemUI.class.getResource("/fxml/diagnoseUI.fxml")));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                center.getChildren().add(anchorPane);

            }
        });


        exitButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                stage.close();

            }
        });


    }

    public StackPane getCenter() {
        return center;
    }

    public void setCenter(StackPane center) {
        this.center = center;
    }

    public Stage getStage() {
        return stage;
    }
}
